﻿using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Model.Result;
using Estsh.Core.Models;
using Estsh.Core.Wms.IServices;
using Microsoft.AspNetCore.Mvc;

namespace Estsh.Core.Web.Plugin.Wms.Areas.wms.Controllers.ReworkAndRepair
{
    [Route("wms/[controller]")]
    [ApiController]
    public class ReworkAndRepairController : Controller
    {
        private IReworkAndRepairService service;
        public ReworkAndRepairController(IReworkAndRepairService _service)
        {
            service = _service;
        }

        [HttpPost("index")]
        public IActionResult Index()
        {
            return View();
        }

        
        /// <summary>
		///查询工作中心（产线）
		/// </summary>
		/// <param name="jobj"></param>
		/// <returns></returns>
		[HttpPost("GetSysPdlineList")]
        public IActionResult GetSysPdlineList(Newtonsoft.Json.Linq.JObject jobj)
        {
            string userId = jobj["userId"].ToString().Trim();
            //string factoryId = jobj["factoryId"].ToString().Trim();

            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<Syspdline_Extend> obj = service.GetSysPdlineList(userId);
                if (obj != null)
                {
                    result.Success = true;
                    result.Data = obj;
                }
                else
                {
                    result.Success = true;
                }
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        ///根据工作中心查询工单
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("GetWorkOrderByPdlineId")]
        public IActionResult GetWorkOrderByPdlineId(Newtonsoft.Json.Linq.JObject jobj)
        {
            string pdlineId = jobj["pdline"].ToString().Trim();
            //string factoryId = jobj["factoryId"].ToString().Trim();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<MesWorkOrder_Extend> obj = service.GetWorkOrderByPdlineId(pdlineId);
                if (obj.Count > 0)
                {
                    result.Success = true;
                    result.Data = obj;
                }
                else
                {
                    result.Msg = "产线下没有工单数据！";
                    result.Success = true;

                }
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        ///扫描条码（查询零件的bom清单）
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("GetBomDetailDownInfo")]
        public IActionResult GetBomDetailDownInfo(Newtonsoft.Json.Linq.JObject jobj)
        {
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string orderNo = jobj["orderNo"].ToString();
                string cartonNo = jobj["cartonNo"].ToString();
                SetObjectDetail obj = service.GetBomDetailDownInfo(orderNo, cartonNo);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);

        }

        /// <summary>
        ///提交 （提交工单）
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("SubmitOrder")]
        public IActionResult SubmitOrder(Newtonsoft.Json.Linq.JObject jobj)
        {
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string orderNo = jobj["orderNo"].ToString();
                string cartonNo = jobj["cartonNo"].ToString();
                string loginId = jobj["loginId"].ToString();
                string partIds = jobj["partIds"].ToString();
                SetObjectDetail obj = service.SubmitOrder(orderNo, cartonNo,partIds, loginId);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);

        }






    }
}
